import 'package:flutter/material.dart';
import 'package:honeypotflutter/publicBussine/bussine_colors.dart';
import 'package:honeypotflutter/publicBussine/bussine_font.dart';
import 'package:honeypotflutter/publicBussine/bussine_image.dart';

class LoginTextInput extends StatefulWidget {
  final String? hint;
  final TextEditingController? controller;
  final FocusNode? node;
  final TextInputType keyboardType;
  final bool isPassWord;
  final Widget? suffixIcon;

  const LoginTextInput({
    super.key,
    this.hint,
    this.controller,
    this.node,
    this.keyboardType = TextInputType.text,
    this.isPassWord = false,
    this.suffixIcon,
  });

  @override
  State<LoginTextInput> createState() => _LoginTextInputState();
}

class _LoginTextInputState extends State<LoginTextInput> {
  /// 是否显示密码
  bool _isHiddle = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (widget.isPassWord) {
      _isHiddle = true;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top: 10),
      child: TextField(
        controller: widget.controller,
        focusNode: widget.node,
        keyboardType: widget.keyboardType,
        decoration: InputDecoration(
            hintText: widget.hint,
            hintStyle: TextStyle(
              fontSize: MGFont.textFont2,
              color: MGColors.text3Color,
            ),
            enabledBorder: UnderlineInputBorder(
                borderSide: BorderSide(
              color: MGColors.splitColor,
            )),
            focusedBorder: UnderlineInputBorder(
                borderSide: BorderSide(
              color: MGColors.splitColor,
            )),
            suffixIcon: suffixIconWidget()),
        style: TextStyle(
          fontSize: MGFont.textFont1,
          color: MGColors.text1Color,
        ),
        obscureText: _isHiddle,
      ),
    );
  }

  Widget? suffixIconWidget() {
    if (widget.suffixIcon != null) {
      return widget.suffixIcon!;
    }

    if (widget.isPassWord == false) {
      return null;
    }

    return IconButton(
      icon: Image.asset(
        _isHiddle ? MGImage.image("public_hiddle") : MGImage.image("public_show"),
        width: 40,
        height: 20,
        fit: BoxFit.contain,
      ),
      onPressed: () {
        setState(() {
          _isHiddle = !_isHiddle;
        });
      },
    );
  }
}
